# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/8/17 10:54
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : drag.py
# @Version: OpenTSN_planner_builder 1.0


import sys
from PyQt5.QtCore import pyqtSignal, QMimeData, Qt
from PyQt5.QtGui import QDrag
from PyQt5.QtWidgets import QApplication, QGridLayout, QGroupBox, QWidget, QPushButton


class MyPushButton(QPushButton):

    def __init__(self, parent=None, text=''):
        super(MyPushButton, self).__init__(parent)
        self.text = text
        self.setText(text)

    def mouseMoveEvent(self, e):
        if e.buttons() != Qt.LeftButton:
            return
        mimeData = QMimeData()  # 拖拽
        mimeData.setText('add,' + self.text)
        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.exec_(Qt.MoveAction)
        self.setAcceptDrops(False)


class MyPushButton2(QPushButton):

    def __init__(self, parent=None, text=''):
        super(MyPushButton2, self).__init__(parent)
        self.text = text
        self.setText(text)

    def mouseMoveEvent(self, e):
        if e.buttons() != Qt.LeftButton:
            return
        mimeData = QMimeData()
        mimeData.setText('move,' + self.text)
        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos() - self.rect().topLeft())
        drag.exec_(Qt.MoveAction)
        self.setAcceptDrops(False)


class MyGroupBox(QGroupBox):
    AddSignal = pyqtSignal(list)
    MoveSignal = pyqtSignal(list)

    def __init__(self, parent=None):
        super(MyGroupBox, self).__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        # print(e)
        if e.mimeData().hasText():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        data = e.mimeData().text().split(',')
        # print((e.pos().x(), e.pos().y()))
        if data[0] == 'add':
            self.AddSignal.emit([data[1], e.pos()])
        elif data[0] == 'move':
            self.MoveSignal.emit([data[1], e.pos()])


class DragDemoView(QWidget):

    def __init__(self):
        super(DragDemoView, self).__init__()
        self.setUI()
        self.show()

    def setUI(self):
        self.setWindowTitle("DragDemoView")
        self.resize(QApplication.desktop().availableGeometry().width() - 400,
                    QApplication.desktop().availableGeometry().height() - 200)

        self.drawBox = MyGroupBox(self)
        self.drawBox.AddSignal.connect(self.add_button)
        self.drawBox.MoveSignal.connect(self.move_button)
        self.toolBox = QGroupBox(self)
        self.button1 = MyPushButton(self.toolBox, 'test')
        self.buttonDict = {}

        self.layout = QGridLayout(self)
        self.layout.addWidget(self.toolBox, 0, 0, 1, 1)
        self.layout.addWidget(self.drawBox, 0, 1, 1, 1)

    def add_button(self, data):
        n = 0
        buttonName = data[0] + str(n)
        while buttonName in self.buttonDict:
            n += 1
            buttonName = data[0] + str(n)

        button = MyPushButton2(self.drawBox, buttonName)
        self.buttonDict[buttonName] = button
        button.move(data[1])
        button.show()

    def move_button(self, data):
        button = self.buttonDict[data[0]]
        button.move(data[1])
        button.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DragDemoView()
    app.exit(app.exec_())

